#include <stdio.h>
#include <list>
#include <iostream>
#include "Robot.h"
#include "MoveEvent.h"
#include "HitPointEvent.h"
#include "World.h"
#include "Scheduler.h"

void Test(GameObject *object)
{
	MoveEvent move;
	HitPointEvent hitpoint;

	move.SetDirection(Direction::EAST);

	object->HandleEvent(&move);

	object->HandleEvent(&hitpoint);

	if (hitpoint.IsSet())
	{
		printf("Hitpoint is %d, %d\n", hitpoint.X(), hitpoint.Y());
	}
}

int main()
{
		//Robot robot;

		//Test(&robot);
	try
	{
		GameObjectFactory factory;
		World world(&factory);
		Scheduler scheduler;
		
	
		world.Resize(6, 6);

		GameObject* robot1 = world.AddRobot("1", 1, 4);
		GameObject* robot2 = world.AddRobot("2", 2, 4);
		GameObject* robot3 = world.AddRobot("3", 4, 4);
		GameObject* robot = robot1;

		char dir;


		MoveEvent moveEvent;
		while(true)
		{ 
			std::cout << "u(p), l(eft), r(ight), d(down), q(uit), 1 2 3 (robot choice)" << std::endl;
			world.Print();
	    
			std::cin >> dir;
			switch(dir)
			{
			case 'u':
				moveEvent.SetDirection(Direction::NORTH);
				scheduler.AddEvent(robot, &moveEvent);
				scheduler.Simulate(&world);
				break;
			case 'd':
				moveEvent.SetDirection(Direction::SOUTH);
				scheduler.AddEvent(robot, &moveEvent);
				scheduler.Simulate(&world);
				break;
			case 'l':
				moveEvent.SetDirection(Direction::WEST);
				scheduler.AddEvent(robot, &moveEvent);
				scheduler.Simulate(&world);
				break;
			case 'r':
				moveEvent.SetDirection(Direction::EAST);
				scheduler.AddEvent(robot, &moveEvent);
				scheduler.Simulate(&world);
				break;
			case '1':
				robot = robot1;
				break;
			case '2':
				robot = robot2;
				break;
			case '3':
				robot = robot3;
				break;
			case 'q':
					return 0;
			default:
				break;
			}
		}
	}
	catch(const char* s)
	{
		printf("Error: %s\n", s);
	}
	catch(...)
	{
		printf("Unknown error\n");
	}

	return 0;
}
